package com.hdcx.Minfresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.adapter.DiscountAdapter;
import com.hdcx.Minfresh.bean.DiscountWrapper;
import com.hdcx.Minfresh.url.MyUrl;
import com.hdcx.Minfresh.util.AppUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/11/18.
 */

public class DiscountActivity extends BaseActivity {
    RecyclerView recyclerView;
    DiscountAdapter adapter;
    Double min;
    public TextView sure;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview_);
        initView();
        initData();
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recycler);
        findViewById(R.id.tv_left).setOnClickListener(this);
        sure = (TextView) findViewById(R.id.tv_right);
        sure.setText("确定");
        ((TextView) findViewById(R.id.tv_title)).setText("优惠券");
    }

    private void initData() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new DiscountAdapter(this);
        adapter.setSureOnclik(sure);
        recyclerView.setAdapter(adapter);
        min = getIntent().getDoubleExtra("min", 0.0);
        post_main();
    }

    /*  参数
    uid		//用户id 20
    min     //付款金额*/
    private void post_main() {
        AppUtil.showLoading(this);
        JSONObject json = new JSONObject();
        try {
            json.put("uid", AppUtil.getUserId());
            json.put("min", min+"");
            Log.e("DiscountActivity", "DiscountActivity--post_main--最小金额"+min);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.postString()
                .url(MyUrl.USERDISCOUNT)
                .content(json.toString())
                .build()
                .execute(new Callback<DiscountWrapper>() {
                    @Override
                    public DiscountWrapper parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        Log.e("DiscountActivity", "DiscountActivity--parseNetworkResponse--抵用券"+json);
                        return new Gson().fromJson(json, DiscountWrapper.class);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                    }

                    @Override
                    public void onResponse(DiscountWrapper response) {
                        AppUtil.closeLoading();
                        if (response.getStatus() == 1) {
                            adapter.setData(response);
                        }
                    }
                });
    }

    @Override
    public void onMyItemClick(String jump, String id) {
        Intent intent = new Intent();
        intent.putExtra("Disname", jump);
        intent.putExtra("id", id);
        setResult(RESULT_OK, intent);
        finish();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_left:
                finish();
                break;
        }
    }
}
